import curd from "../../common/services/curd";
import { getRandom } from "../../utils/common";
import { GETCLASSNAME, GETSUBJECTSTOTAL } from "../class/sql";
import {
  ADDANSWER,
  ADDANSWERSUBJECT,
  ADDOPTIONS,
  ADDSUBJECT,
  ADDSUBJECTS,
  CREATEPAPER,
  DELPAPER,
  DELSUBJECT,
  GETALLPAPER,
  GETALLPAPERNAMES,
  GETALLSUBJECTS,
  GETANSWER,
  GETANSWERDETAIL,
  GETERROPTIONS,
  GETEXEC,
  GETONEPAPER,
  GETOPTIONS,
  GETSCORE,
  GETSTUPAPER,
  GETSUBJECTRECORD,
  INCCORR,
  INCERR,
  ISPAPEREXIST,
  SEARCHSUBJECT,
} from "./sql";
class PaperService {
  /**
   * 添加题目
   * @param paperId
   * @param subjectId
   */
  async addSubject(paperId: string, subjectId: string) {
    return await curd.add(ADDSUBJECT, paperId, subjectId);
  }

  /**
   * 创建答卷
   * @param name
   * @param userId
   * @param classId
   */
  async createPaper(
    name: string,
    userId: string,
    classId: string,
    time: string
  ) {
    return (await curd.addRes(CREATEPAPER, name, userId, classId, time))
      .insertId;
  }

  /**
   * 获取教师创建的所有答卷
   */
  async getAll(userId: string) {
    return await curd.get(GETALLPAPER, userId);
  }

  /**
   * 获取一个答卷
   */
  async getOnePaper(paperId: string) {
    return await curd.get(GETONEPAPER, paperId);
  }

  /**
   * 通过名字获取一个答卷
   * @param name
   */
  async isPaperExistByName(name: string) {
    return await curd.isExist(ISPAPEREXIST, name);
  }

  /**
   * 获取试题
   */
  async getAllSubjects() {
    return await curd.get(GETALLSUBJECTS);
  }

  /**
   * 删除答卷
   */
  async delPaper(id: string) {
    return await curd.delete(DELPAPER, id);
  }

  /**
   * 添加选项
   * @param args
   * @returns
   */
  async addOptions(a: string, b: string, c: string, d: string) {
    return (await curd.addRes(ADDOPTIONS, a, b, c, d)).insertId;
  }

  /**
   * 添加题目
   * @param options_id
   * @param ans
   * @param score
   * @param name
   */
  async addSubjects(
    options_id: string,
    ans: string,
    score: string,
    name: string,
    analysis: string
  ) {
    return await curd.add(ADDSUBJECTS, options_id, ans, score, name, analysis);
  }

  /**
   * 删除题目
   * @param id
   */
  async delSubject(id: string) {
    return await curd.delete(DELSUBJECT, id);
  }

  /**
   * 存储答题的试卷
   * @param args
   * @returns
   */
  async addAnswer(
    user_id: string,
    time: string,
    paper_id: string,
    score: string
  ) {
    return (await curd.addRes(ADDANSWER, user_id, time, paper_id, score))
      .insertId;
  }

  /**
   * 添加答题的试题
   */
  async addAnswerSubject(
    subject_id: string,
    ans: string,
    uAns: string,
    paperId: string,
    userId: string
  ) {
    return await curd.add(
      ADDANSWERSUBJECT,
      subject_id,
      ans,
      uAns,
      paperId,
      userId
    );
  }

  /**
   * 获取用户答题情况
   * @param userId
   * @returns
   */
  async getAnswer(userId: string) {
    return await curd.get(GETANSWER, userId);
  }

  /**
   * 正确题 + 1
   * @param id
   * @returns
   */
  async incCorr(id: string) {
    return curd.update(INCCORR, id);
  }

  /**
   * 错误题 + 1
   * @param id
   */
  async incErr(id: string) {
    return curd.update(INCERR, id);
  }

  /**
   *
   * @param id
   */
  async getAnswerDetail(id: string) {
    return curd.get(GETANSWERDETAIL, id, id, id);
  }

  /**
   * 获取题目记录
   */
  async getSubjectRecord(id: string) {
    return curd.get(GETSUBJECTRECORD, id);
  }

  /**
   * 获取学生的答卷
   */
  async getStuPapers(userId: string) {
    const res = await curd.get(GETCLASSNAME, userId);
    if (res.length) {
      let data = await curd.get(GETSTUPAPER);
      data = data.filter((d: any) => d.classInfo.name === res[0].name)
      return data
    } else {
      return [];
    }
  }

  /**
   * 搜题
   * @param w
   */
  async searchSubject(w: string) {
    w = "%" + w + "%";
    return await curd.get(SEARCHSUBJECT, w);
  }

  /**
   * 获取练习
   */
  async getExec(limit = 10) {
    let [{ count }] = await curd.get(GETSUBJECTSTOTAL);
    count = count ? (count - limit < 0 ? 0 : count - limit) : 0;
    const offset = getRandom(0, count);
    return await curd.get(GETEXEC, limit, offset);
  }

  /**
   * 获取老师的考试详情
   * @param paperId 
   */
  async getTchDataAna(paperId: string) {
    const score= await curd.get(GETSCORE, paperId)
    const error = await curd.get(GETERROPTIONS, paperId)
    const subjects = await curd.get(GETOPTIONS, paperId)
    return {
      score,
      error,
      subjects
    }
  }

  /**
   * 获取所有试卷的名字
   * @param userId 
   * @returns 
   */
  async getAllPaperName(userId: string) {
    return curd.get(GETALLPAPERNAMES, userId)
  }
}

export default new PaperService();
